package com.liiy.missyou.api.v1;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.liiy.missyou.bo.PageCounter;
import com.liiy.missyou.exception.http.NotFoundException;
import com.liiy.missyou.model.Banner;
import com.liiy.missyou.model.Spu;
import com.liiy.missyou.service.BannerService;
import com.liiy.missyou.service.SpuService;
import com.liiy.missyou.util.CommonUtil;
import com.liiy.missyou.vo.PagingDozer;
import com.liiy.missyou.vo.SpuSimplifyVO;
import jakarta.validation.constraints.Positive;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/spu")
@Validated
public class SpuController {

    @Autowired
    private SpuService spuService;

    @GetMapping("/{id}")
    @ResponseBody
    public Spu getDetail(@PathVariable @Positive Long id){
        Spu spu = spuService.getDetail(id);
        if(spu == null){
            throw new NotFoundException(30003);
        }

        return spu;
    }

    @GetMapping("/{id}/simplify")
    @ResponseBody
    public SpuSimplifyVO getSimplifyDetail(@PathVariable @Positive Long id){
        Spu spu = spuService.getDetail(id);
        if(spu == null){
            throw new NotFoundException(30003);
        }
        SpuSimplifyVO vo = new SpuSimplifyVO();
        BeanUtils.copyProperties(spu, vo);

        return vo;
    }

    @GetMapping("/latest")
    @ResponseBody
    public PagingDozer<Spu, SpuSimplifyVO> getLatestSpuList(
            @RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "10") Integer count){
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start,count);
        Page<Spu> page =  this.spuService.getLatestPagingSpu(pageCounter.getPage(),pageCounter.getCount());

        return new PagingDozer<>(page, SpuSimplifyVO.class);
    }

    @GetMapping("/category/{id}")
    @ResponseBody
    public PagingDozer<Spu, SpuSimplifyVO> getByCategoryId(
            @PathVariable @Positive(message="{id.positive}") Long id,
            @RequestParam(name="is_root",defaultValue = "false") Boolean is_root,
            @RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "10") Integer count){
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start,count);
        Page<Spu> page =  this.spuService.getByCategory(id,is_root,pageCounter.getPage(),pageCounter.getCount());

        return new PagingDozer<>(page, SpuSimplifyVO.class);
    }
}